package edu.uaskl.jqf.tools;

public class MathTools {

    /** Epsilon to compare floating point. Value: {@value} */
    public static final double EPSILON = 10E-6; // TODO could be more precise? -tbach

    /** Value of 1/sqrt(2) */
    public static final double ONE_OVER_SQRT_2 = 1.0 / Math.sqrt(2);

    /** Returns the array [0, 1, 2, 3, 4, 5, .. , size - 1] */
    public static int[] intArrayFilledwithIndicesOfSize(final int size) {
        final int array[] = new int[size];
        for (int i = 0; i < size; ++i)
            array[i] = i;
        return array;
    }

    public static boolean isDoubleCloseToDoule(final double a, final double b) {
        return Math.abs(a - b) <= EPSILON; // TODO not the best check, could be improved with relative & null check. See literature about fp -tbach
    }

    public static int modularExponentiationBaseExpMod(final int base, final int exponent, final int modulus) {
        long result = 1;
        for (int i = 31; i >= 0; --i) {
            result = (result * result) % modulus;
            if ((exponent & (1 << i)) != 0)
                result = (result * base) % modulus;
        }
        return (int) result; // will not truncate since modulus is an int -tbach
    }
}
